if (window.plus) {
    ready();
} else {
    document.addEventListener('plusready', ready, false);
}
$(function(){
  if(AGENT_CLIENT.WEB){
    ready();
  }
})

function ready() {
  var
  id = getUrlParam('id'),
  uploadControl = {},
  order = {},
  user = {},                 //用户信息  
  html = [],                 //html字符串
  thisPage = {};             //当前页

  var afterSalePage = function () {
    this.init();
  };
  afterSalePage.prototype = {
    init: function () {
      this.loadData();
      this.bindEvent();
    },
    loadData:function(){
      user = getLocalStorage(STORAGE_MANAGER.user);
      if (!user) {
        redirect("../account/login.html");
        return false;
      }
      if( !id ){
        $.toast("订单号不存在");
        return;
      }
      $.ajax({
        type: "GET",
        url: CONFIG_MANAGER.RequstUrl + "v1/orders/" + id,
        async: true,
        headers: { "token": user.token },
        beforeSend: function () {
          $.showWaiting("等待中");
        }
      }).done(function (data) {       
        order = data;
        thisPage.loadOrderDetail(data);
      }).always(function(data){
        $.closeWaiting();
      }).fail(function(data){
        $.toast(data);
      });
    },
    loadOrderDetail:function(data){
      var goods = data.items;
      html = [];
      
      $.each(goods, function(i, item) {
        html.push('<div name="goodsItem" tip="'+ item.sku_id +'" class="mbtopbottom sqshbg" style="margin-top:0px;">');
        html.push('<div class="mbbottom with100 overflow">');
        html.push('<dl class="cpxqxztop cpxqxztop1 mtop0">');
        var src = getImageUrl(item.logo, 65, 65);
        html.push('<dt><img src="'+ src +'" /></dt>');
        html.push('<dd>');
        html.push('<p class="floatr"><span class="jinbi"></span><span class="money">&yen;'+ item.act_price +'</span></p>');
        html.push(item.sku_name);
        html.push('</dd>');
        html.push('<dd class="huisezi"><span class="x2">x'+ item.qty +'</span>'+ item.specs +'</dd>');
        html.push('</dl>');
        html.push('</div>');
        html.push('<div class="sqshnr mbbottom">');
        html.push('<b class="sqsl">申请数量</b>');
        html.push('<p name="addOrMinus_'+ item.sku_id +'" class="gwslny sqshslk">');
        html.push('<span name="minus" class="mbright1">-</span>');
        html.push('<span name="qty" tip="'+ item.qty +'" class="slsrk">1</span>');
        html.push('<span name="add" class="mbleft">+</span>');
        html.push('</p>');
        html.push('</div>');
        html.push('<p class="sqshnr mbbottom">');
        html.push('<b class="sqsl">申请类型</b>');
        html.push('<span name="apply_type_'+ item.sku_id +'" class="xxlyxz xxlyxz1 sqshslk">');
        html.push('<span>');
        html.push('<b class="checkboxFour">');
        html.push('<input id="return_'+ i +'" name="'+ item.sku_id +'" type="radio" value="1" class="checkbox-gx"/>');
        html.push('<label for="return_'+ i +'"></label>');
        html.push('</b>退货</b>');
        html.push('</span>');
        html.push('<span>');
        html.push('<b class="checkboxFour">');
        html.push('<input id="change_'+ i +'" name="'+ item.sku_id +'" type="radio" value="2" class="checkbox-gx" />');
        html.push('<label for="change_'+ i +'"></label>');
        html.push('</b>换货');
        html.push('</span>');
        html.push('<span>');
        html.push('<b class="checkboxFour">');
        html.push('<input id="repaire_'+ i +'" name="'+ item.sku_id +'" type="radio" value="3" class="checkbox-gx" />');
        html.push('<label for="repaire_'+ i +'"></label>');
        html.push('</b>维修');
        html.push('</span>');
        html.push('</span>');
        html.push('</p>');
        html.push('<div class="pjddbg">');
        html.push('<p class="pjddbt"><b>问题描述</b></p>');
        html.push('<p class="pjddk">');
        html.push('<textarea name="description" cols="" rows="" placeholder="说写什么吧~" class="pjddwb"></textarea>');
        html.push('</p>');
        html.push('</div>');
        html.push('</div>');
      });
      html.push('<div class="mbtopbottom sqshbg" style="margin-top:-10px;">');
      html.push('<p class="fxxfxdtu whitebg">');
      //html.push('<span><b class="yguanbi"><i class="iconfont icon-diancha"></i></b><img src="../../images/dplbtx.jpg"></span>');
//    html.push('<span class="tianjiaan" style="position:relative">');
//    html.push('<input id="imageforAfterSale" style="height: 70px;" class="a_upload" type="file" capture="camera" accept="image/png,image/gif,image/jpg,image/jpeg">');
//    html.push('<img src="../../images/tjantu.png"></span>');
      
      html.push('<span name="selectUploadType" class="tianjiaan" style="position: relative;">');
      if(AGENT_CLIENT.WEB){
        html.push('<input name="addImgFile" class="a_upload" type="file" capture="camera" accept="image/png,image/gif,image/jpg,image/jpeg" style="height: 70px;"/>');
      }
      html.push('<img src="../../images/tjantu.png" />');
      html.push('</span>');       
      
      
      html.push('</p>');
      html.push('</div>');
      html.push('<p id="commit" class="anniu"><a href="javascript:void(0);">提交</a></p>');      
      $("#main").html(html.join(''));
      //上传绑定
      uploadControl = new uploadPage();      
      uploadControl.bindUploadShows();
    },
    bindEvent:function(){
      
      $("#back").on("click", function(){
      	back();
      });
      
      //添加减少数目
      this.addOrMinusAction();
      
      //提交
      this.commit();
    },
    addOrMinusAction:function(){
      //添加
      $("#main").on("click", "p[name^='addOrMinus'] span:even", function(){
        var add = ( "add" == $(this).attr('name') );
        var qtySpan = $(this).parent().find("span[name='qty']");
        var count = parseInt( qtySpan.attr('tip') );
        var qty = parseInt( qtySpan.html() );
        if(add){
          if(qty<count){
            qty++;
          }
        }else{
          if( qty>1 ){
            qty--;
          }
        }
        qtySpan.html(qty);
      });
    },
    commit:function(){
      $("#main").on("click", "#commit", function(){
        var requestData = {
          order_id:order.id,
          member_id:user.id,
          region:order.region,
          address:order.address,
          consignee:order.consignee,
          phone:order.mobile,
          return_type:1,
          img:[],
          items:[],
        };
        $.each($("img[name='addImage']"), function(i, img) {
            var index = $(this).attr("src").indexOf(',');
            var newStrings = $(this).attr("src").substr(index+1);        
        	  requestData.img.push( newStrings );
        });
        $.each($("div[name='goodsItem']"), function(i, div) {
        	var item = {
        	  sku_id:$(this).attr("tip"),
        	  description:$(this).find("textarea[name='description']").val(),
        	  remark:"",
        	  qty:$(this).find("span[name='qty']").html(),
        	  type:$(this).find("input[type=radio]:checked").val(),
        	};
        	requestData.items.push(item);
        });
        
        $.ajax({
        	type:"POST",
        	url: CONFIG_MANAGER.RequstUrl + "v1/orders/after/appadd",
        	async:true,
        	contentType: 'application/json',
        	data: JSON.stringify(requestData),
        	headers:{ token:user.token },
          	beforeSend:function(){ $.showWaiting(); }
        }).done(function(data){
          redirect("aftersaleorders.html?status=all");
        }).always(function(){
          $.closeWaiting();
        }).fail(function(data){
          $.toast(data);
        });
      
      });
    }
  }
    
  thisPage = new afterSalePage();
}
        